// pages/food/food.js
const db = wx.cloud.database()
const $ = db.command.aggregate
Page({
  data: {
    totalMoney:0,
    totalNum: 0,
    cartList: [],
    isHide: true
  },
  /*生命周期函数--监听页面加载*/
  onLoad(options) {
    this.getTables()
   
    let cartList = wx.getStorageSync('cart') || []
    this.setData({
      cartList:cartList
    })
    console.log('店名name是：',options.name_lxj);
    wx.cloud.callFunction({
      name:'getFoodList_lxj',
      data:{
        canteen:options.name_lxj
      }
    }).then(res=>{
      console.log('获取菜品列表成功',res.result.data);
      let list = res.result.data
      list.forEach(item=>{
        if(cartList && cartList.length>0){
          let result = cartList.find(cart=>{
            return cart._id == item._id
          })
          if(result){
            item.num = result.num
          }else{
            item.num = 0
          }
        }else{
          item.num = 0
        }
      })
      this.setData({
        foodlist:res.result.data
      })
      this.getTotal()
    })
  },
  // 获取分类
  getTables(){
    db.collection('food').aggregate()
    .group({
      _id: '$fenlei',
      // num: $.sum(1)
    })
    .end()
    .then(res=>{
      console.log('菜品分类：',res);
      this.setData({
        tabs:res.list
      })
    })
  },
  //计算总价格和总数量
  getTotal(){
    let cartList = this.data.cartList
    let totalMoney = 0
    let totalNum = 0
    cartList.forEach(item=>{
      totalMoney+=item.price*item.num
      totalNum+=item.num
    })
    this.setData({
      totalMoney,
      totalNum
    })
  },
  //点击增减菜品
  subNum(e){
    let id = e.currentTarget.dataset.id
    console.log('减',id);
    let list = this.data.foodlist
    let  cartList = this.data.cartList
    list.forEach(item=>{
      console.log('便利了',item.name);
      if(item._id == id){
        if(item.num > 0)
        {
          item.num --
          var index = cartList.findIndex(cart=>{   //find方法 查询购物车数组cartList里面是否有当前点击的菜品
              return cart._id == id;
            })
            if(index > -1){
              cartList[index].num = item.num
            }
            // console.log('点击的菜品是否在购物车里',index);
            if(item.num == 0){  //当菜品数量为0时，移除
              cartList.splice(index,1) //删除下标为index的元素
            }
        }
        else{
          wx.showToast({
            icon:'none',
            title: '已经不能再减啦！',
          })
        }
      }
    })
    console.log('当前菜品列foodlist',list)
    this.setData({
      foodlist:list,
      cartList,
    })
    this.getTotal()
    wx.setStorageSync('cart', cartList)
    console.log('购物车',cartList);
  },
  addNum(e){
    let id = e.currentTarget.dataset.id
    console.log('加',id);
    let list = this.data.foodlist
    let  cartList = this.data.cartList
    list.forEach(item=>{
      // console.log('便利了',item.name);
      if(item._id == id){
        item.num ++; 
        if(cartList && cartList.length > 0){
          var result = cartList.find(cart=>{   //find方法 查询购物车数组cartList里面是否有当前点击的菜品
            return cart._id == id;
          })
          console.log('点击的菜品是否在购物车里',result);
          if(result){
            result.num = item.num
          }else{
            cartList.push(item)
          }
        }else{
          cartList.push(item)
        }
      }
    })
    console.log('当前菜品列foodlist',list)
    console.log('购物车',cartList);
    this.setData({
      foodlist:list,
      cartList,
    })
    this.getTotal()
     wx.setStorageSync('cart', cartList)
  },
  closeMask(){
    this.setData({
      isHide: true
    }) 
  },
  openCart(){
    this.setData({
      isHide: false
    })
  },
  //清空购物车
  clearCart(){
    let foodlist = this.data.foodlist
    foodlist.forEach(item=>{
      item.num = 0
    })
    this.setData({
      foodlist,
      cartList:[],
      totalMoney:0,
      totalNum:0
    })
    wx.setStorageSync('cart', null)
  },
  //删除购物车中某一项
  deleteItem(e){
    console.log(e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index
    let cartList = this.data.cartList
    let foodlist = this.data.foodlist
    foodlist.forEach(item=>{
      if(cartList[index]._id == item._id){
        item.num = 0
      }
    })
    cartList.splice('index',1)
    this.setData({
      cartList,
      foodlist
    })
    this.getTotal()
    wx.setStorageSync('cart', cartList)
  },
  //去支付
  goPay(){
    wx.navigateTo({
      url: '/pages/pay/pay',
      
    })
  },
  onShareAppMessage:function(){

  }
})